import Snake from './Snake'
import MoveArea from './MoveArea'
import ModeBoard from './ModeBoard'
class ScoreBoard {
  private progressEle: HTMLElement
  private snakeLengthEle: HTMLElement
  private progressBarEle: HTMLElement
  private progressNum: number = 0
  constructor() {
    this.progressEle = document.getElementById('scoreboard-progress')!
    this.snakeLengthEle = document.getElementById('scoreboard-snake-len')!
    this.progressBarEle = document.getElementById('scoreboard-progressBar')!
    this.resetStatus()
  }

  /**
   * 0到100的数 当前游戏进度
   */
  get currentProgressNum() {
    return this.progressNum
  }

  /**
   * 更新面板信息
   */
  public updateInfo() {
    this.snakeLengthEle.innerText = Snake.snakeLength.toString()
    this.progressNum = (Snake.snakeLength / MoveArea.gridsNums) * 100
    this.progressEle.innerText = this.progressNum.toFixed(2).toString() + ' %'
    this.progressBarEle.style.right =
      (100 - this.progressNum - 1).toString() + '%'
  }

  /**
   * 重设状态
   */
  public resetStatus() {
    this.progressNum = 0
    this.progressEle.innerText = '0 %'
    this.snakeLengthEle.innerText = '1'
    this.progressBarEle.style.right = '100%'
  }

  /**
   * 是否挑战成功
   * 会根据当前选择的难度模式进行判断
   * @param num 0到100的数,如果当前进度大于num,则视为游戏成功
   * @returns
   */
  public challengeSuccess(num: number = 10) {
    if (ModeBoard.gameMode == 'simple') {
      num = 10
    }
    if (ModeBoard.gameMode == 'advance') {
      num = 30
    }
    if (ModeBoard.gameMode == 'difficult') {
      num = 50
    }
    if (this.progressNum > num) {
      return true
    }
    return false
  }
}

export default new ScoreBoard()
